package org.example.dev.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.example.dev.mapper.ImageEditMapper;
import org.example.dev.mapper.ImageGenerationMapper;
import org.example.dev.mapper.UserMapper;
import org.example.dev.pojo.ImageEdits;
import org.example.dev.pojo.ImageGeneration;
import org.example.dev.pojo.PageBean;
import org.example.dev.pojo.User;
import org.example.dev.service.IImageService;
import org.example.dev.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author 李佳宸
 */

@Service
@RequiredArgsConstructor
public class ImageServiceImpl implements IImageService {

    private final ImageGenerationMapper imageGenerationMapper;
    private final ImageEditMapper imageEditMapper;
    private final IUserService userService;


    @Override
    // 插入图片生成详情
    public void insertImageGeneration(ImageGeneration imageGeneration) {
        imageGenerationMapper.insert(imageGeneration);
    }

    @Override
    // 插入图片编辑记录
    public void insertImageEdits(ImageEdits imageEdits) {
        imageEditMapper.insert(imageEdits);
    }

    @Override
    // 根据页码获取图片编辑记录，分页
    public PageBean<ImageEdits> getImageEditsByPageId(Integer currentPage, Integer pageSize) {
        Page<ImageEdits> page = new Page<>(currentPage,pageSize);
        QueryWrapper<ImageEdits> queryWrapper=new QueryWrapper<>();
        //执行分页查询
        IPage<ImageEdits> imageEditsIPage = imageEditMapper.selectPage(page,queryWrapper);
        //将分页结果进行转化
        PageBean<ImageEdits> imageEditsPageBean = new PageBean<>(imageEditsIPage.getTotal(),imageEditsIPage.getRecords());
        return imageEditsPageBean;
    }

    @Override
    // 根据页码获取图片编辑记录，分页
    public Page<ImageGeneration> getImageGenerationsByPageId(Integer currentPage, Integer pageSize) {
        Integer userId = userService.getCurrentUser().getUserId();
        LambdaQueryWrapper<ImageGeneration> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(ImageGeneration::getUserId ,userId);
        //执行分页查询
        Page<ImageGeneration> page= imageGenerationMapper.selectPage(new Page<>(currentPage,pageSize),queryWrapper);
        return page;
    }

    @Override
    // 根据Id获取图片生成详情
    public ImageGeneration getImageDetail(String generationId) {
        return imageGenerationMapper.selectById(generationId);
    }

    //根据传过来的url插入生成详情和编辑记录
    @Override
    public void insertImage(String imageUrl,String description,Integer type, Integer width, Integer height){
        String uuid=UUID.randomUUID().toString();
        if(type == 0) {//文生图生成记录创建
            //创建ImageGeneration对象
            ImageGeneration imageGeneration = new ImageGeneration();
            imageGeneration.setGenerationId(uuid);
            imageGeneration.setGeneratedImageUrl(imageUrl);
            imageGeneration.setInputText(description);
            imageGeneration.setGenerationTime(LocalDateTime.now());
            imageGeneration.setType(type);
            imageGeneration.setWidth(width);
            imageGeneration.setHeight(height);
            imageGeneration.setUserId(userService.getCurrentUser().getUserId());

            //插入图片生成记录
            insertImageGeneration(imageGeneration);

//            //创建ImageEdits对象
//            ImageEdits imageEdits = new ImageEdits();
//            imageEdits.setGenerationId(uuid);
//            imageEdits.setEditDescription(description);
//            imageEdits.setEditedImageUrl(imageUrl);
//            insertImageEdits(imageEdits);
//            //插入图片编辑记录
//            imageEditMapper.insert(imageEdits);
        }
        if(type == 1) {//图生图生成记录创建
            //创建ImageGeneration对象
            ImageGeneration imageGeneration = new ImageGeneration();
            imageGeneration.setGenerationId(uuid);
            imageGeneration.setGeneratedImageUrl(imageUrl);
            imageGeneration.setInputImageUrl(description);
            imageGeneration.setGenerationTime(LocalDateTime.now());
            imageGeneration.setType(type);
            imageGeneration.setWidth(width);
            imageGeneration.setHeight(height);
            imageGeneration.setUserId(userService.getCurrentUser().getUserId());

            //插入图片生成记录
            insertImageGeneration(imageGeneration);

//            //创建ImageEdits对象
//            ImageEdits imageEdits = new ImageEdits();
//            imageEdits.setGenerationId(uuid);
//            imageEdits.setEditDescription(description);
//            imageEdits.setEditedImageUrl(imageUrl);
//            insertImageEdits(imageEdits);
//            //插入图片编辑记录
//            imageEditMapper.insert(imageEdits);
        }
    }

    public void deleteImageGeneration(String id){
        imageGenerationMapper.deleteById(id);
    }
}
